package com.android.questions.ui.view;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowInsets;
import android.widget.FrameLayout;

/**
 * FileName: MainFrameLayout
 * Author: admin
 * Date: 2021/11/1 20:04
 * Description:
 */
public class MainFrameLayout extends FrameLayout {

    public MainFrameLayout(Context context) {
        this(context, null);
    }

    public MainFrameLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MainFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
            @Override
            public void onChildViewAdded(View parent, View child) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
                    requestApplyInsets();
                }
            }

            @Override
            public void onChildViewRemoved(View parent, View child) {
            }
        });
    }

    @Override
    public WindowInsets onApplyWindowInsets(WindowInsets insets) {
        int childCount = getChildCount();
        for (int index = 0; index < childCount; index++)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
                getChildAt(index).dispatchApplyWindowInsets(insets);
            }
        return insets;
    }
}